package light.mvc.controller.idea;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import light.mvc.controller.base.BaseController;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.sys.EmployInfoVO;
import light.mvc.pageModel.sys.SendEmailVO;
import light.mvc.pageModel.sys.SendLogVO;
import light.mvc.pageModel.tba.MamProductVO;
import light.mvc.service.sys.EmployInfoServiceI;
import light.mvc.service.sys.SendLogServiceI;
import light.mvc.service.tba.MamProductServiceI;
import light.mvc.utils.SendEmailUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/idea")
public class sendEmail extends BaseController {
	
	@Autowired
	private MamProductServiceI mamProductService;
	
	@Autowired
	private EmployInfoServiceI employInfoService;
	
	@Autowired
	private SendLogServiceI sendLogService;
	
	@RequestMapping("/sendEmail")
	@ResponseBody
	public Json sendEmail(String ids) {
		
		
		Json j = new Json();
		SendLogVO sendLogVO=new SendLogVO();
		try {
			ids= StringEscapeUtils.unescapeHtml4(ids);
			JSONObject postData = JSONObject.fromObject(ids);
			JSONArray idlist = postData.getJSONArray("ids");
			Iterator<Object> it = idlist.iterator();

			String id="";
			while (it.hasNext()) {
				JSONObject ob = (JSONObject) it.next();
				id+=ob.getInt("id")+",";
			}
			id=id.substring(0,id.length() - 1);
			
			List<EmployInfoVO> listEmployInfoVO=employInfoService.findByEmployInfoVOList(id);
			if(listEmployInfoVO==null){
				j.setSuccess(false);
				j.setMsg("没有淘宝客商品！");
				return j;
			}
			String emailAll="";
			for(EmployInfoVO employInfoVO:listEmployInfoVO){
				emailAll+=employInfoVO.getEmployEmail()+",";
			}
			//emailAll=emailAll.substring(0,emailAll.length()-1);
			List<MamProductVO> listMamProductVO = mamProductService.findByAll();
			String title="充电宝解决你用电困难！";
			String str="";
			
			if(listMamProductVO.size()==1){
				str="<p>您好</p>"+
						"<p>"+listMamProductVO.get(0).getMaProduct()+"</p>"+
						"<p>电脑用户请复制下面的网址，粘帖到浏览器地址栏打<br>"+
						"<a href=\"http://"+listMamProductVO.get(0).getMaUrl()+"\">"+listMamProductVO.get(0).getMaUrl()+"</a></p>"+
							"<p>手机用户请复制下面的淘口令，用淘宝或者天猫APP打开<br>"+
							""+listMamProductVO.get(0).getMaPwd()+"</p>"+
							"<p>请注意使用时间，以免失效。</p>"+
							"<p>祝您心情愉快</p>";
			}else{
				String strContent="";
				for(MamProductVO mamProductVO:listMamProductVO){
					strContent += "<tr><td>" + mamProductVO.getMaProduct() + "</td><td>" + mamProductVO.getMaUrl() + "</td><td>" + mamProductVO.getMaPwd() + "</td></tr>";
				}
				str="<p>您好</p>"+
						"<p>电脑用户请复制下面的网址，粘帖到浏览器地址栏打</p>"+
						"<p>手机用户请复制下面的淘口令，用淘宝或者天猫APP打开</p>";
				str+="<style type=\"text/css\">table.gridtable {font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px;border-color: #666666;border-collapse: collapse;}table.gridtable th {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #dedede;}table.gridtable td {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;}</style>";
				str+="<table class=\"gridtable\"><tr><th>产品</th><th>电脑连接</th><th>手机淘口令</th></tr>"+ strContent + "</table>";
				str+="<p>请注意使用时间，以免失效。</p>";
				str+="<p>祝您心情愉快</p>";
			}
			String contant="<html><head><meta http-equiv=Content-Type content=text/html; charset=gb2312></head><body>"+ str + "</body></html>";
			
			SendEmailVO sendEmailVO = new SendEmailVO();
			sendEmailVO.setToEmail(emailAll);
			sendEmailVO.setTitle(title);
			sendEmailVO.setContant(contant);
			SendEmailUtil seu=new SendEmailUtil();
			
			sendLogVO.setSendTheme("淘宝客推送");
			sendLogVO.setSendThemeStyle("tbaSendEmail");
			sendLogVO.setSendTime(new Date());
			sendLogVO.setSendDestination(emailAll);
			
			if(seu.sendmail(sendEmailVO)){
				sendLogVO.setSendStatus(1);
				j.setSuccess(true);
				j.setMsg("发送成功！");
			}else{
				sendLogVO.setSendStatus(0);
				j.setSuccess(false);
				j.setMsg("发送失败！");
			}
			sendLogService.add(sendLogVO);
			
		} catch (Exception e) {
			System.out.println(e);
			j.setMsg(e.getMessage());
		}
		return j;
	}
}
